-- masterOogway
-- Created by dengc Mar/29/2016
-- 乌龟大师

-- 初始化
function initMasterOogway(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local function okFunc(classId)
                -- 执行提交
                DungeonActionM.go("master_oogway", self.gridData:getPos(), classId);
            end

            -- 打开建筑提交界面
            local bonus = FormulaM.invoke("CALC_MASTER_OOGWAY_BONUS_LIST", ME.user);
            self:openBuildingSubmit(okFunc, EQUIP_OPEN_TYPE_SUBMIT_ALL, bonus);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
end

-- 乌龟大师提交事件
function onMasterOogwaySubmit(self, classId, bonus, isSucc)
    local name = ItemM.query(classId, "name");
    if not isSucc then
        -- 失败了

        -- 播放建筑强光（较弱）
        playBuildingStrongLight(self, true);

        -- 弹字提示
        local msg = getLocStr("master_oogway_submit_fail");
        local alertList = splitString(msg, "\n");
        splitAlert(alertList);
    else
        -- 成功了

        -- 播放建筑强光
        playBuildingStrongLight(self);

        -- 获取提示语
        local function getMasterOogwayDesc(classId)
            local desc = "master_oogway_desc_" .. classId;
            return getLocStr(desc);
        end

        local msg = getMasterOogwayDesc(classId);
        local alertList = splitString(msg, "\n");
        splitAlert(alertList);

        local remainGrid = false;
        if self.gridData.multi_submit == 1 then
            remainGrid = true;
        end
        self:onBuildingBonus(bonus, remainGrid);
    end
end
